<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>tunnel</title>

    <style>
        #up {
            width: 600px;
            height: 500px;
            position: fixed;
            top: 10vh;
            left: 50%;
            margin-left: -300px;
            background-color: rgba(66, 185, 131, .5);
            line-height: 300px;
            text-align: center;
            display: none;
            border-radius: 10px;
        }
    </style>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }


        body {
            font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
            width: 100%;
            height: 100vh;
            overflow: hidden;
            background-color: #2C2C2C;
        }



        .box {
            display: flex;
            flex-direction: column;
            width: 550px;
            margin: 10vh auto;
            background-color: #F9F9F9;
            height: 80vh;
            box-sizing: border-box;
            border-radius: 5px;
        }

        @media screen and (max-width:500px) {
            .box {
                width: 100%;
                height: 60vh;
            }
        }

        .box .header {
            position: relative;
            display: flex;
            width: 100%;
            border-bottom: 1px solid #e3e4e5;
            display: flex;
        }

        .box .header #count {
            color: #191919;
            font-size: 20px;
            line-height: 68px;
            padding-left: 20px;
        }

        .box .header #error {
            position: absolute;
            top: 5px;
            text-align: center;
            width: 100%;
            color: #f00;
        }

        .box .content {
            flex: 6;
        }

        .box .content #msg {
            width: 100%;
            height: 100%;
            border: none;
            outline: none;
            font-size: 14px;
            font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
            padding: 10px;
            box-sizing: border-box;
        }

        .box .footer {
            border-top: 1px solid #e3e4e5;
            flex: 2;
            display: flex;
            flex-direction: column;
            box-sizing: border-box;
            padding: 0px 18px;
        }

        .box .footer .top {
            flex: 2;
        }

        .box .footer .top #upload {
            display: none;
        }

        .box .footer .top img {
            cursor: pointer;
            padding: 5px 0px;
        }

        .box .footer .mid {
            flex: 4;
        }

        .box .footer .mid #input {
            background-color: #F9F9F9;
            width: 100%;
            height: 100%;
            border: none;
            outline: none;
            font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;

        }

        .box .footer .bot {
            flex: 4;
            display: flex;
        }

        .box .footer .bot .space {
            flex: 1;
        }

        .box .footer .bot button {
            border: none;
            cursor: pointer;
            width: 64px;
            height: 32px;
            border-radius: 4px;
            color: rgb(7, 193, 96);
            font-size: 30rpx;
            padding: 0px 24px;
            box-sizing: content-box;
            background-color: #ECECEC;
            margin: 10px 0px;
        }

        #progress {
            height: 2px;
            background-color: #1AAD19;
            width: 0%;
        }
    </style>
</head>

<body>
    <script src="./socket.io/socket.io.js"></script>
    <script src="./js/axios.min.js"></script>
    <div id="app">
        <div class="box">

            <div class="header">
                <div><span id="count"></span></div>
                <div id="error">

                </div>
            </div>
            <div class="content">
                <textarea name="" id="msg"></textarea>

                <div id="up">

                </div>
            </div>
            <div id="progress"></div>
            <div class="footer">
                <div class="top">
                    <input id="upload" name="" id="" type="file">
                    <img src="./icons/upload.png" alt="" width="30px" height="30px" onclick="clickUploadIcon()">
                </div>
                <div class="mid">
                    <textarea name="" id="input"></textarea>
                </div>
                <div class="bot">
                    <div class="space"></div>
                    <button onclick="send_msg()" type="button">同步消息</button>
                </div>
            </div>
        </div>
    </div>
    <div id="med">

    </div>
    <script>
        let uploadFileName = '';
        let $ = document.querySelectorAll.bind(document);
        let sock = io();
        sock.on("connect", (socket) => {
            $("#error")[0].innerHTML = "";
            console.log("连接了", socket);
        });
        sock.on("sync_count", count => {
            console.log(count);
            $("#count")[0].innerHTML = `当前房间人数 ${count}`;
        })

        let old_msg = "";
        function send_msg() {
            let input = $("#input")[0].value
            let msg = $("#msg")[0].value;
            if (input) msg = msg + "\n" + input;
            if (old_msg == msg) return false;
            old_msg = msg;
            $("#msg")[0].value = msg;
            $("#input")[0].value = '';

            console.log("传输了", msg);
            sock.emit("send_msg", msg);
        }

        document.onkeydown = function (ev) {
            if (ev.ctrlKey && ev.key == 's') {
                send_msg();
                return false;
            }
        }

        sock.on("sync_msg", msg => {
            console.log("接到消息", msg);
            $("#msg")[0].value = msg;
        })
        sock.on("disconnect", () => {
            $("#error")[0].innerHTML = "服务器失去连接";
        });

        sock.on("download", (dir, filename) => {
            if (uploadFileName == filename)
                return;
            console.log(`收到文件`)
            let oA = document.createElement("a");
            oA.href = `./${dir}/${filename}`;
            oA.click();
            setTimeout(() => {
                location.reload()
            }, 2000)
        });
        sock.on("open_url", (url) => {
            // let oA = document.createElement("a");
            // oA.target = "_blank"
            // oA.href = url;
            // oA.click();
            let oMed = document.getElementById("med");
            oMed.innerHTML = ""
            oMed.innerHTML = `<iframe src="${url}" style="position: absolute;bottom: 0px;" frameborder="0" id="frame" width="100%" height="80px"></iframe>`;

        });

    </script>
    <script>
        show = function () {
            uploadBox.style.display = "block"
        }
        let hide = function () {
            uploadBox.style.display = "none"
        }
    </script>
    <script>
        let uploadBox = document.getElementById("up");
        let timer = null

        document.ondragover = function () {
            clearTimeout(timer);
            show();
            timer = setTimeout(hide, 300)
            return false
        }
        document.ondrop = function () {
            return false
        }

        uploadBox.ondragleave = function () {
            uploadBox.innerHTML = "请将文件拖至此处"
            console.log("离开盒子");
        }
        uploadBox.ondragover = function (ev) {
            uploadBox.innerHTML = "请松手"
            ev.preventDefault();
        }

        function uploadFile(files) {
            let formdata = new FormData();
            Array.from(files).forEach(file => {
                // console.log(file);
                formdata.append('f1', file)
                uploadFileName = file.name
                axios.post("./upload", formdata, {
                    onUploadProgress: progressEvent => {
                        let persent = (progressEvent.loaded / progressEvent.total * 100 | 0)		//上传进度百分比
                        document.getElementById("progress").style.width = `${persent}%`
                        console.log(persent);

                    },
                })
            });
        }

        uploadBox.ondrop = function (ev) {
            uploadBox.innerHTML = "请将文件拖至此处"
            files = ev.dataTransfer.files;
            if (files.length == 0) {
                console.log("读取失败");
                ev.preventDefault();
            } else if (files[0].name.endsWith('.url')) {
                // console.log(files);
                console.log("打开网址");
            } else {
                uploadFile(files);
            }
            ev.preventDefault();
        }
    </script>
    <script>

        let clickUploadIcon = () => {
            document.getElementById("upload").click();
        }
        document.getElementById("upload").onchange = function () {
            uploadFile(this.files)
        }
    </script>
</body>

</html>